<script type="text/javascript">

  const formJSON_{{ random }} = {{{ formJSON }}}

  const formValuesToLoad_{{ random }} = {{{ formValuesToLoad }}}

  //used for json explorer
  var formValuesToLoad = {{{ formValuesToLoad }}}
  var editMode = {{{ editMode }}}
  var currentForm = '';
  var formIsDirty = false;
  
      loadModuleSettingForm = async function (){
        ('setting for dirty false')
      formIsDirty = false;
        return form_{{ random }}();
      }

      form_{{ random }} = async function () {
          Formio.icons = 'fontawesome';
          formService.setFormApiUrls(Formio);

          Formio.{{formioFunction}}(document.getElementById('formio_{{ random }}'), formJSON_{{ random }}).then((form) => {
              currentForm = form; //make form available in other JS
              form.options.noAlerts = true;
              console.log('log form', form);

              if (formValuesToLoad_{{ random }}) {
                  form.submission = {
                      data: formValuesToLoad_{{ random }}
                  };
              }


              form.on('submit', async function (submission) {

                  // analyticsTrackEvent({event: 'FORM_SUBMIT', data: submission.data});

                  {{{ onFormSubmitFunction }}}

                  form.emit('submitDone')//call submitdone event here to stop loading

              });

              form.on('change', function (changed) {
                if (typeof formChanged != "undefined") {
                    formChanged(changed);
                  }

                  if (typeof pageBuilderFormChanged != "undefined") {
                    pageBuilderFormChanged(changed);
                  }

                  if (changed.components) {
                    contentTypeComponents = changed.components;
                    console.log("event ->", event);
                  }

                  try {
                    setupAdminMediaFormImage();
                  } catch (error) {
                    //anon mode
                  }
              });

          });
      };

      window.addEventListener("load", function() {
          console.log('Page is loaded');
          if (typeof module === 'undefined') {
            form_{{ random }}();
          }
      });

</script>

<div id="formio_{{ random }}"></div>
